package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;

import com.zzyl.entity.ProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NusringPlanExprMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NusringPlanExprMapper nusringPlanExprMapper;


    /**
     * 分页查询护理计划
     *
     *
     * @param status
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageResponse selectByPage(String plan_name, Integer status, Integer pageNum, Integer pageSize) {
        //设置分页
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> nursingPlanVos=nursingPlanMapper.selectByPag(plan_name, status);
        return PageResponse.of(nursingPlanVos, NursingProjectVo.class);

    }

    @Override
    public void add(ProjectPlanDto projectPlanDto) {
        NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(projectPlanDto, NursingProjectPlan.class);
        nursingPlanMapper.insert(nursingProjectPlan);

        List<ProjectPlan> planList = nursingProjectPlan.getProjectPlans();
        if (!CollectionUtils.isEmpty(planList)){
            planList.forEach(item->{
                item.setPlanId(Math.toIntExact(nursingProjectPlan.getId()));
            });
        }
        nusringPlanExprMapper.insertBatch(planList);
    }
    /**
     * 根据id查询
     * @param id
     * @return
     */

    @Override
    public NursingPlanVo findByid(Integer id) {
        NursingPlan nursingPlan = nursingPlanMapper.findByid(id);
        return BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        nursingPlanMapper.delete(id);
    }

    /**
     * 启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanVo> selecAll() {
        List<NursingPlanVo> nursingPlanVos = nursingPlanMapper.selectAl();
        return  nursingPlanVos;

    }

}




